<?php $layout = 'admin';
$title = '管理面板 - MyCMS'; ?>

<!-- 统计卡片 -->
<div style="display: grid; grid-template-columns: repeat(auto-fit, minmax(250px, 1fr)); gap: 1.5rem; margin-bottom: 2rem;">
    <div class="card">
        <div class="card-body" style="background: linear-gradient(135deg, #667eea 0%, #764ba2 100%); color: white;">
            <div style="display: flex; justify-content: space-between; align-items: center;">
                <div>
                    <h3 style="margin: 0 0 0.5rem; font-size: 2.5rem; font-weight: bold;"><?= $stats['total_posts'] ?></h3>
                    <p style="margin: 0; opacity: 0.9;">文章总数</p>
                </div>
                <div style="font-size: 3rem; opacity: 0.3;">📝</div>
            </div>
        </div>
    </div>

    <div class="card">
        <div class="card-body" style="background: linear-gradient(135deg, #f093fb 0%, #f5576c 100%); color: white;">
            <div style="display: flex; justify-content: space-between; align-items: center;">
                <div>
                    <h3 style="margin: 0 0 0.5rem; font-size: 2.5rem; font-weight: bold;"><?= $stats['total_users'] ?></h3>
                    <p style="margin: 0; opacity: 0.9;">用户总数</p>
                </div>
                <div style="font-size: 3rem; opacity: 0.3;">👥</div>
            </div>
        </div>
    </div>

    <div class="card">
        <div class="card-body" style="background: linear-gradient(135deg, #4facfe 0%, #00f2fe 100%); color: white;">
            <div style="display: flex; justify-content: space-between; align-items: center;">
                <div>
                    <h3 style="margin: 0 0 0.5rem; font-size: 1.5rem; font-weight: bold;"><?= $stats['system_info']['memory_usage'] ?></h3>
                    <p style="margin: 0; opacity: 0.9;">内存使用</p>
                </div>
                <div style="font-size: 3rem; opacity: 0.3;">💾</div>
            </div>
        </div>
    </div>

    <div class="card">
        <div class="card-body" style="background: linear-gradient(135deg, #fa709a 0%, #fee140 100%); color: white;">
            <div style="display: flex; justify-content: space-between; align-items: center;">
                <div>
                    <h3 style="margin: 0 0 0.5rem; font-size: 1.5rem; font-weight: bold;"><?= $stats['today_stats']['posts_created'] ?></h3>
                    <p style="margin: 0; opacity: 0.9;">今日新增文章</p>
                </div>
                <div style="font-size: 3rem; opacity: 0.3;">📅</div>
            </div>
        </div>
    </div>
</div>

<!-- 内容区域 -->
<div style="display: grid; grid-template-columns: 2fr 1fr; gap: 2rem; margin-bottom: 2rem;">
    <!-- 最近文章 -->
    <div class="card">
        <div class="card-header">
            <h2>最近文章</h2>
            <a href="/admin/posts" class="btn btn-sm">查看全部</a>
        </div>
        <div class="card-body">
            <?php if (empty($stats['recent_posts'])): ?>
                <p style="color: #666; text-align: center; padding: 2rem;">暂无文章</p>
            <?php else: ?>
                <div style="display: grid; gap: 1rem;">
                    <?php foreach ($stats['recent_posts'] as $post): ?>
                        <div style="display: flex; justify-content: space-between; align-items: center; padding: 1rem; background: #f8f9fa; border-radius: 4px;">
                            <div>
                                <h4 style="margin: 0 0 0.5rem;">
                                    <a href="/posts/<?= $post['id'] ?>" style="color: #333; text-decoration: none;">
                                        <?= htmlspecialchars($post['title']) ?>
                                    </a>
                                </h4>
                                <small style="color: #666;">
                                    发布于 <?= date('Y-m-d H:i', strtotime($post['created_at'])) ?>
                                </small>
                            </div>
                            <div style="display: flex; gap: 0.5rem;">
                                <a href="/posts/<?= $post['id'] ?>/edit" class="btn btn-sm btn-secondary">编辑</a>
                                <a href="/posts/<?= $post['id'] ?>" class="btn btn-sm">查看</a>
                            </div>
                        </div>
                    <?php endforeach; ?>
                </div>
            <?php endif; ?>
        </div>
    </div>

    <!-- 最近活动 -->
    <div class="card">
        <div class="card-header">
            <h2>最近活动</h2>
        </div>
        <div class="card-body">
            <?php if (empty($recentActivity)): ?>
                <p style="color: #666; text-align: center; padding: 2rem;">暂无活动</p>
            <?php else: ?>
                <div style="display: grid; gap: 0.8rem;">
                    <?php foreach ($recentActivity as $activity): ?>
                        <div style="display: flex; align-items: center; padding: 0.8rem; background: #f8f9fa; border-radius: 4px;">
                            <div style="margin-right: 0.8rem; font-size: 1.2rem;">
                                <?= $activity['type'] === 'post' ? '📝' : '👤' ?>
                            </div>
                            <div style="flex: 1;">
                                <p style="margin: 0 0 0.2rem; font-size: 0.9rem; color: #333;">
                                    <?= htmlspecialchars($activity['description']) ?>
                                </p>
                                <small style="color: #666;">
                                    <?= date('m-d H:i', strtotime($activity['created_at'])) ?>
                                </small>
                            </div>
                        </div>
                    <?php endforeach; ?>
                </div>
            <?php endif; ?>
        </div>
    </div>
</div>

<!-- 日志统计 -->
<div class="card">
    <div class="card-header">
        <h2>日志统计</h2>
        <a href="/admin/logs" class="btn btn-sm">查看日志</a>
    </div>
    <div class="card-body">
        <div style="display: grid; grid-template-columns: repeat(auto-fit, minmax(200px, 1fr)); gap: 1rem;">
            <div style="text-align: center; padding: 1rem; background: #f8f9fa; border-radius: 4px;">
                <div style="font-size: 1.5rem; font-weight: bold; color: #007bff; margin-bottom: 0.5rem;">
                    <?= $logStats['access_log_lines'] ?>
                </div>
                <div style="font-size: 0.9rem; color: #666;">访问日志条数</div>
                <div style="font-size: 0.8rem; color: #999; margin-top: 0.2rem;">
                    (<?= $logStats['access_log_size'] ?>)
                </div>
            </div>

            <div style="text-align: center; padding: 1rem; background: #f8f9fa; border-radius: 4px;">
                <div style="font-size: 1.5rem; font-weight: bold; color: #28a745; margin-bottom: 0.5rem;">
                    <?= $logStats['app_log_lines'] ?>
                </div>
                <div style="font-size: 0.9rem; color: #666;">应用日志条数</div>
                <div style="font-size: 0.8rem; color: #999; margin-top: 0.2rem;">
                    (<?= $logStats['app_log_size'] ?>)
                </div>
            </div>

            <div style="text-align: center; padding: 1rem; background: #f8f9fa; border-radius: 4px;">
                <div style="font-size: 1.5rem; font-weight: bold; color: #ffc107; margin-bottom: 0.5rem;">
                    <?= $logStats['rate_limit_entries'] ?>
                </div>
                <div style="font-size: 0.9rem; color: #666;">限速记录数</div>
            </div>
        </div>
    </div>
</div>

<!-- 系统信息 -->
<div class="card" style="margin-top: 2rem;">
    <div class="card-header">
        <h2>系统信息</h2>
        <a href="/admin/settings" class="btn btn-sm">系统设置</a>
    </div>
    <div class="card-body">
        <div style="display: grid; grid-template-columns: repeat(auto-fit, minmax(300px, 1fr)); gap: 1.5rem;">
            <div>
                <h4 style="margin: 0 0 1rem; color: #555;">运行环境</h4>
                <table class="table">
                    <tr>
                        <td style="font-weight: 500;">PHP 版本</td>
                        <td style="color: #666;"><?= htmlspecialchars($stats['system_info']['php_version']) ?></td>
                    </tr>
                    <tr>
                        <td style="font-weight: 500;">Web 服务器</td>
                        <td style="color: #666;"><?= htmlspecialchars($stats['system_info']['server_software']) ?></td>
                    </tr>
                    <tr>
                        <td style="font-weight: 500;">数据库版本</td>
                        <td style="color: #666;"><?= htmlspecialchars($stats['system_info']['database_version']) ?></td>
                    </tr>
                    <tr>
                        <td style="font-weight: 500;">执行时间限制</td>
                        <td style="color: #666;"><?= htmlspecialchars($stats['system_info']['max_execution_time']) ?></td>
                    </tr>
                </table>
            </div>

            <div>
                <h4 style="margin: 0 0 1rem; color: #555;">资源配置</h4>
                <table class="table">
                    <tr>
                        <td style="font-weight: 500;">内存使用</td>
                        <td style="color: #666;"><?= htmlspecialchars($stats['system_info']['memory_usage']) ?></td>
                    </tr>
                    <tr>
                        <td style="font-weight: 500;">内存限制</td>
                        <td style="color: #666;"><?= htmlspecialchars($stats['system_info']['memory_limit']) ?></td>
                    </tr>
                    <tr>
                        <td style="font-weight: 500;">上传限制</td>
                        <td style="color: #666;"><?= htmlspecialchars($stats['system_info']['upload_max_filesize']) ?></td>
                    </tr>
                    <tr>
                        <td style="font-weight: 500;">可用空间</td>
                        <td style="color: #666;"><?= htmlspecialchars($stats['system_info']['disk_free']) ?></td>
                    </tr>
                </table>
            </div>
        </div>
    </div>
</div>